﻿using System;
using System.Web.Mvc;
using CodeFinder.Common.Entities;
using CodeFinder.Common.Interfaces;
using CodeFinder.Web.Helper;
using CodeFinder.Web.Models;

namespace CodeFinder.Web.Controllers {
    public class PostController : Controller {
        private IPostService m_postService;
        public PostController(IPostService service) {
            m_postService = service;
        }


        //
        // GET: /Post/Details/5

        public ViewResult Details(int id) {
            Post post = m_postService.FindPostById(id);

            return View(post);
        }

        //
        // GET: /Post/Create

        public ActionResult Create() {
            var post = new Post();
            ViewBag.Categories = new SelectList(DataHelper.GetAllCategories(), "Id", "DisplayText");
            ViewBag.ViewScopes = new SelectList(DataHelper.GetAllViewScopes(), "Id", "DisplayText");
           
            post.UserId = 1;
            return View(post);
        }

        //
        // POST: /Post/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Post post) {
            post.CreatedOn = DateTime.Now;
            if (ModelState.IsValid) {
                post.Content = SanitizeHelper.SanitizeHtml(post.Content);
                m_postService.Save(post);
                return RedirectToAction("Index", "Home");
            }

            ViewBag.Categories = new SelectList(DataHelper.GetAllCategories(), "Id", "DisplayText", post.Category);
            ViewBag.ViewScopes = new SelectList(DataHelper.GetAllViewScopes(), "Id", "DisplayText", post.ViewScope);
            return View(post);
        }

        //
        // GET: /Post/Edit/5

        public ActionResult Edit(int id) {
            Post post = m_postService.FindPostById(id);
            ViewBag.Categories = new SelectList(DataHelper.GetAllCategories(), "Id", "DisplayText", post.Category);
            ViewBag.ViewScopes = new SelectList(DataHelper.GetAllViewScopes(), "Id", "DisplayText", post.ViewScope);
            return View(post);
        }

        //
        // POST: /Post/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Post post) {
            if (ModelState.IsValid) {
                m_postService.Save(post);
                return RedirectToAction("Index", "Home");
            }
            ViewBag.Categories = new SelectList(DataHelper.GetAllCategories(), "Id", "DisplayText", post.Category);
            ViewBag.ViewScopes = new SelectList(DataHelper.GetAllViewScopes(), "Id", "DisplayText", post.ViewScope);
            return View(post);
        }

        //
        // GET: /Post/Delete/5

        public ActionResult Delete(int id) {
            Post post = m_postService.FindPostById(id);
            return View(post);
        }

        //
        // POST: /Post/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id) {
            m_postService.Delete(id);
            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Adds the comment.
        /// </summary>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public PartialViewResult AddComment(CommentModel comment) {
            Comment cm = new Comment {
                Name = comment.Name,
                Email = comment.Email,
                FollowByEmail = comment.Follow,
                Content = comment.Content,
                PostId = comment.PostId,
                CreatedOn = DateTime.Now
            };
            m_postService.AddComment(cm);
            ViewData["Index"] = m_postService.CountComments(comment.PostId);
            return PartialView("Comment", cm);
        }

    }
}
